import * as types from '../mutation-types';
import Vue from 'vue';
import moment from '@/utils/Moment.js';
import Tools from '@/utils/Tools.js';
const state = {
	token: null,
	info: {
		avatar: {
			file_path: ''
		}
	},
	referee: {
		timestamp: '',
		code: ''
	},
	setting: {
		chatSingleSound: true,
		chatGroupSound: true
	},
	matchmaker: null,

}

const mutations = {
	[types.UPDATE_USER_TOKEN](state, token) {
		state.token = token
	},
	[types.UPDATE_USER_INFO](state, info) {
		if (info.login_identity != state.info.login_identity) {
			uni.$emit('switchIdentity', { identity: info.login_identity })
			Tools.switchTabber(info.login_identity);
		}
		if (info.login_identity == 10 && info.master_data.like_count > 0) {
			uni.showTabBarRedDot({
				index: 4
			})
		} else {
			uni.hideTabBarRedDot({
				index: 4
			})
		}
		state.info = info;
	},
	[types.EXIT_AND_CLEAR](state) {
		state.token = null;
		state.info = {
			info: {
				avatar: {
					file_path: ''
				}
			}
		}
	},
	[types.UPDATE_REFEREE_CODE](state, refereeCode = '') {
		state.referee = {
			timestamp: Date.parse(new Date()) / 1000 + 30 * 60,
			code: refereeCode
		}
	},
	[types.UPDATE_MEMBER_SETTING](state, setting) {
		state.setting = Object.assign(state.setting, setting);
	},
	[types.UPDATE_MEMBER_MATCHMAKER](state) {
		Vue.prototype.$u.api.getMyCupid().then(res => {
			const matchmaker = res.data.cupid || null;
			if (matchmaker) {
				Vue.set(matchmaker, 'bindtime', moment(matchmaker.bindtime, 'YYYY-MM-DD HH:mm:ss').format('YYYY年MM月DD日'));
				Vue.set(matchmaker, 'logintime', moment(matchmaker.logintime, 'YYYY-MM-DD HH:mm:ss').format('YYYY年MM月DD日 HH:mm'));
			}
			console.log(state)
			state.matchmaker = matchmaker;
		});
	},


}

const getters = {
	getRefereeCode: state => {
		if (Date.parse(new Date()) / 1000 < state.referee.timestamp) {
			return state.referee.code
		}
		return ''
	}
}

//统一登录IM方法
const loginIM = (state) => {

}


export default {
	state,
	mutations,
	getters
}
